package org.goyo.main.services.storage.impl;

import java.util.List;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.goyo.common.baseBean.Constants;
import org.goyo.common.exception.DBException;
import org.goyo.common.exception.ServiceException;
import org.goyo.common.utils.DateUtils;
import org.goyo.common.utils.Pager;
import org.goyo.main.domain.dao.report.ISysReport;
import org.goyo.main.domain.dao.storage.IInventoryUnpackDao;
import org.goyo.main.domain.entity.report.InventoryReport;
import org.goyo.main.domain.entity.storage.InventoryUnpack;
import org.goyo.main.domain.entity.storage.InventoryUnpackEntry;
import org.goyo.main.domain.entity.storage.PurInStorage;
import org.goyo.main.domain.entity.storage.PurInStorageEntry;
import org.goyo.main.domain.entity.system.SysUser;
import org.goyo.main.services.BaseService;
import org.goyo.main.services.storage.IInventoryUnpackService;
import org.springframework.stereotype.Service;
@Service
@Transactional
public class InventoryUnpackServiceImpl   extends BaseService implements IInventoryUnpackService{
	@Resource
	private IInventoryUnpackDao inventoryUnpackDao;

	@Override
	public Pager<InventoryUnpack> findByCondition(String where, Pager pager)
			throws DBException, ServiceException {
		
		return  inventoryUnpackDao.findByCond(where, pager);
	}

	@Override
	public InventoryUnpack saveOrUpdateInfo(InventoryUnpack info, Object[] uuids,SysUser user)
			throws DBException, ServiceException {
		String uuid = null;
		//保存用户数据
		info.setStatus(Constants.SAVESTATUS);
		info.setAlterDate(DateUtils.getCurrentDate());
		info.setAlterUser(user);
		if(info.getUuid() == null || "".equals(info.getUuid())){
			info.setUuid(null);
			info.setCreateDate(DateUtils.getCurrentDate());
			info.setCreateUser(user);
//			alterBillNumber(InventoryUnpack.BIZTYPE, info.getNumber());
			uuid = inventoryUnpackDao.save(info);
		}else{
			inventoryUnpackDao.update(info);
			uuid = info.getUuid();
		}
		//删除数据
		if(uuids != null && uuids[0] != null){
			inventoryUnpackDao.delBatch(InventoryUnpackEntry.class, "uuid", uuids);
		}
		info.setUuid(uuid);
		return info;
	}

	@Override
	public List<InventoryUnpackEntry> findByParentId(String parentId) throws DBException,
			ServiceException {
		return inventoryUnpackDao.findByParentId(parentId);
	}
	
	
	
	@Override
	public void delByUuids(Object[] uuids) throws DBException, ServiceException {
		//批量删除
		for(Object uuid : uuids){
			InventoryUnpack info = inventoryUnpackDao.findByUuid(InventoryUnpack.class, uuid.toString());
			inventoryUnpackDao.delete(info);
		}
	}

	@Override
	public InventoryUnpack submitByUuids(Object[] uuids,SysUser user) throws DBException,
			ServiceException {
		InventoryUnpack info = null;
		//批量提交
		for(Object uuid : uuids){
			info = inventoryUnpackDao.findByUuid(InventoryUnpack.class, uuid.toString());
			info.setAlterDate(DateUtils.getCurrentDate());
			info.setAlterUser(user);
			info.setSubmitDate(DateUtils.getCurrentDate());
			info.setSubmitUser(user);
			info.setStatus(Constants.SUBMITTATUS);
			inventoryUnpackDao.update(info);
		}
		return info;
	}

	@Override
	public InventoryUnpack auditByUuids(Object[] uuids,SysUser user) throws DBException,
			ServiceException {
		InventoryUnpack info = null;
		//批量审核
		for(Object uuid : uuids){
			info = inventoryUnpackDao.findByUuid(InventoryUnpack.class, uuid.toString());
			info.setAlterDate(DateUtils.getCurrentDate());
			info.setAlterUser(user);
			info.setAuditDate(DateUtils.getCurrentDate());
			info.setAuditUser(user);
			info.setStatus(Constants.AUDITSTATUS);
			inventoryUnpackDao.update(info);
		}
		return info;
	}
	
	@Override
	public InventoryUnpack unAuditByUuids(Object[] uuids,SysUser user) throws DBException,
			ServiceException {
		InventoryUnpack info = null;
		//批量反审核
		for(Object uuid : uuids){
			info = inventoryUnpackDao.findByUuid(InventoryUnpack.class, uuid.toString());
			info.setAlterDate(DateUtils.getCurrentDate());
			info.setAlterUser(user);
			info.setAuditDate(null);
			info.setAuditUser(null);
			info.setSubmitUser(null);
			info.setSubmitDate(null);
			info.setStatus(Constants.SAVESTATUS);
			inventoryUnpackDao.update(info);
		}
		return info;
	}

	@Override
	public InventoryUnpack findByUuuid(String uuid) throws DBException,
			ServiceException {
		InventoryUnpack info = inventoryUnpackDao.findByUuid(InventoryUnpack.class, uuid);
		return info;
	}
}
